package com.hx.controller;

import com.hx.dto.SysUserDTO;
import com.hx.pojo.SysUser;
import com.hx.service.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/user")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    /**
     * 查询所有用户 返回视图页面
     * @param model
     * @return
     */
    @GetMapping(value = {"/findAllUser","/"})
    public String findAllUser(Model model){
        List<SysUser> allUser = sysUserService.findAllUser();
        model.addAttribute("users",allUser);
        return "userManage";
    }

    /**
     * 办理入职页面跳转
     * @param model
     * @return
     */
    @GetMapping("/in")
    public String userIn(Model model){
        return "userIn";
    }

    /**
     * 保存用户 并跳转回首页列表
     * 进行数据校验
     * @param sysUserDTO
     * @param bindingResult
     * @return
     */
    @PostMapping("/saveUser")
    public String saveUser(@Valid SysUserDTO sysUserDTO,
                           BindingResult bindingResult,
                           Model model){
        if (bindingResult.hasErrors()){
            List<FieldError> allErrors = bindingResult.getFieldErrors();
            for (FieldError fieldError : allErrors) {
                if("name".equals(fieldError.getField())){
                    model.addAttribute("nameError",fieldError.getDefaultMessage());
                }
                if("no".equals(fieldError.getField())){
                    model.addAttribute("noError",fieldError.getDefaultMessage());
                }
                if("position".equals(fieldError.getField())){
                    model.addAttribute("positionError",fieldError.getDefaultMessage());
                }
                System.out.println(fieldError.getField()+"_"+fieldError.getDefaultMessage());
            }
            return "userIn";
        }

        sysUserService.saveUser(sysUserDTO);

        List<SysUser> allUser = sysUserService.findAllUser();
        model.addAttribute("users",allUser);
        return "userManage";
    }

    /**
     * 办理离职页面
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/out")
    public String userOut(Model model,
                          @RequestParam("id") String id){
        SysUser user = sysUserService.findUserById(Integer.valueOf(id));
        model.addAttribute("user",sysUserService.convertUserToUserDTO(user));
        return "userOut";
    }

    /**
     * 更新用户状态
     * @param sysUserDTO
     * @return
     */
    @PostMapping("/updateUserStatus")
    public String updateUserStatus( SysUserDTO sysUserDTO,Model model){

        sysUserDTO.setStatus("已离职");
        sysUserService.updateUserStatus(sysUserDTO);

        List<SysUser> allUser = sysUserService.findAllUser();
        model.addAttribute("users",allUser);
        return "userManage";
    }

    /**
     * 更新用户信息,并页面跳转
     * @param id
     * @return
     */
    @GetMapping("/updateUserInfo")
    public String updateUserInfo(@RequestParam("id") String id,
                                 Model model){

        SysUser user = sysUserService.findUserById(Integer.valueOf(id));
        model.addAttribute("user",sysUserService.convertUserToUserDTO(user));

        return "userUpdate";
    }

    /**
     * 更新用户信息
     * @param
     * @return
     */
    @PostMapping("/updateUser")
    public String updateUserInfo(SysUserDTO sysUserDTO,
                                 Model model){

        sysUserService.updateUserInfo(sysUserDTO);

        List<SysUser> allUser = sysUserService.findAllUser();
        model.addAttribute("users",allUser);
        return "userManage";
    }

}
